#pragma once
#include <set>
#include <list>
#include <string>
#include <memory>
#include <mutex>
#include "BackupFSDefines.h"
/*
Encapsulation of database operations, such as adding records, 
deleting records, updating records, etc., converting various file 
operations into operations on SQLite databases
*/
namespace BackupFS
{
	class BackupFSFileNode;
	class BackupFSSql
	{
	public:
		static bool AddFileMeta(void* vdb, const std::shared_ptr<BackupFSFileNode>& file);
		static bool AddDataBlock(void* vdb, const std::shared_ptr<DATA_BLOCK>& block,LONGLONG node_id);
		static bool UpdateDataBlock(void* vdb, const std::shared_ptr<DATA_BLOCK>& block);
		static bool RemoveFile(void* vdb, const std::shared_ptr<BackupFSFileNode>& file);
		static bool UpdateFileSecurity(void* vdb, LONGLONG node_id, byte* data,LONGLONG size);
		static bool UpdateFileTime(void* vdb, LONGLONG node_id, LONGLONG create_time,LONGLONG write_time,LONGLONG access_time);
		static bool UpdateFileSize(void* vdb, LONGLONG node_id,LONGLONG file_size);
		static bool UpdateFileName(void* vdb, LONGLONG node_id, const std::wstring &file_name,LONGLONG parent_id);
		static bool UpdateChildrenFileName(void* vdb, const std::wstring &old_file_name,const std::wstring& file_name);
		static std::shared_ptr<BackupFSFileNode> GetFileNode(void* vdb, const std::wstring& file_name);
		static bool GetAllChildren(void* vdb, const std::wstring file_name, std::set <std::shared_ptr<BackupFSFileNode>>& files);
		static bool GetAllDataBlocks(void* vdb, LONGLONG file_id, std::list<std::shared_ptr<DATA_BLOCK>>& blocks);
		static bool UpdateCurrentNodeId(void* vdb, LONGLONG node_id);
		static bool UpdateCurrentBlockId(void* vdb, LONGLONG block_id);
		static bool UpdateCurrentFSSize(void* vdb, LONGLONG size);
		static bool UpdateCurrentUsedSize(void* vdb, LONGLONG size);
		static bool GetFsInfo(void* vdb, FS_INFO& fsinfo);
		static bool Begin(void* vdb);
		static bool Commit(void* vdb);
		static void Stop(void* vdb);
	private:
		static bool _UpdateFsINFO(void* vdb, const std::string& field_name, LONGLONG val);
		static std::recursive_mutex _commit_mutex;
	};
}

